package ws.very.util.lang

object StopWatch {
  def apply[R](fn: => R)(timeFn: Long => Unit): R = {
    val (r, t) = withMillTake(fn)
    timeFn(t)
    r
  }
  def withMillTake[R](fn: => R) = withTimeTake(fn)(System.currentTimeMillis())



  private def withTimeTake[R](fn: => R)(getTime: => Long) = {
    val time = getTime
    fn -> (getTime - time)
  }


  def andPrintln[R](actionName:String)(act: =>R)=this(act){t=>
    println(actionName+" takes "+t)
  }

}
